package com.plian.system.controller.transfer;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.plian.system.config.AppKeySign;
import com.plian.system.dto.transfer.DataPlatSign;
import com.plian.system.dto.transfer.IndicatorSign;
import com.plian.system.entity.fr.bank.BankKey;
import com.plian.system.entity.fr.indicators.FinanceIndicators;
import com.plian.system.entity.fr.indicators.FinanceIndicatorsSupplement;
import com.plian.system.entity.fr.indicators.IndicatorsValue;
import com.plian.system.entity.sys.Org;
import com.plian.system.mapper.fr.indicators.FinanceIndicatorsMapper;
import com.plian.system.model.R;
import com.plian.system.model.token.PassToken;
import com.plian.system.service.fr.bank.IBankKeyService;
import com.plian.system.service.fr.financeindicators.IFinanceIndicatorsSupplementService;
import com.plian.system.service.fr.indicatorsvalue.IndicatorsValueService;
import com.plian.system.service.sys.orgzation.IOrgService;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @Author: Take-off
 * @Description: TODO
 * @Date: 2022/11/8 3:18 PM
 * @Version: 1.0
 **/
@RestController
@RequestMapping("api/indicatorTransfer")
@AllArgsConstructor
@Api(value = "指标数据-输出", tags = "指标数据输出")
@Slf4j
public class IndicatorTransferController {

    private IBankKeyService bankKeyService;

    private FinanceIndicatorsMapper indicatorsMapper;

    private IFinanceIndicatorsSupplementService supplementService;

    private IOrgService orgService;

    private IndicatorsValueService indicatorsValueService;
    /**
     * 初始化指标定义
     * @param dataPlatSign
     */
    @PostMapping(value = "/indicators-init")
    @PassToken
    @AppKeySign
    public R<List<FinanceIndicators>> indicatorsInit(@RequestBody DataPlatSign dataPlatSign){
        log.debug("toklen:" + dataPlatSign.getToken());
        List<FinanceIndicators> financeIndicators = indicatorsMapper.selectList(null);
        return R.data(financeIndicators);
    }

    /**
     * 初始化指标定义补充信息
     * @param dataPlatSign
     */
    @PostMapping(value = "/supplement-indicators-init")
    @PassToken
    @AppKeySign
    public R<List<FinanceIndicatorsSupplement>> supplementIndicatorsInit(@RequestBody DataPlatSign dataPlatSign){
        log.debug("toklen:" + dataPlatSign.getToken());
        List<FinanceIndicatorsSupplement> list = supplementService.list();
        return R.data(list);
    }
    /**
     * 初始化指标值
     * @param indicatorSign
     */
    @PostMapping(value = "/indicators-value")
    @PassToken
    @AppKeySign
    public R<List<IndicatorsValue>> indicatorsValues(@RequestBody IndicatorSign indicatorSign){
        log.debug("toklen:" + indicatorSign.getToken());
        String appId = indicatorSign.getAppId();
        BankKey bankKey = bankKeyService.getOne(Wrappers.<BankKey>lambdaQuery().eq(BankKey::getAppId, appId));
        String topGroupId = bankKey.getTopGroupId();
        List<Org> allChild = orgService.getAllChild(topGroupId);
        List<String> orgIds = allChild.stream().map(Org::getId).collect(Collectors.toList());

        List<IndicatorsValue> list = indicatorsValueService.list(new LambdaQueryWrapper<IndicatorsValue>().in(IndicatorsValue::getOrgId, orgIds)
                .eq(Optional.ofNullable(indicatorSign.getYear()).isPresent(),IndicatorsValue::getFyear, indicatorSign.getYear())
                .eq(Optional.ofNullable(indicatorSign.getMonth()).isPresent(),IndicatorsValue::getFmonth, indicatorSign.getMonth())
                .in(Optional.ofNullable(indicatorSign.getIndicatorsIds()).isPresent(), IndicatorsValue::getIndicatorsId, indicatorSign.getIndicatorsIds())
        );
        return R.data(list);
    }

}
